function bvqxtools_makelibs(hPrg, hBar, pRng)
% bvqxtools_makelibs  - compile MEX functions (mex/compiler needed!)
%
% FORMAT:       bvqxtools_makelibs;
%
% No input/output fields.
%
% Note: this should create the following (system dependent) files:
%
%       @transimg/private/renderc.EXT
%       bvqx_srf_parseneighborsc.EXT
%       bvqx_srf_writeneighborsc.EXT
%       clustercoordsc.EXT
%       conv3d.EXT
%       cov_nd.EXT
%       findfirst.EXT
%       flexinterpn.EXT
%       flexmask.EXT
%       floodfill3c.EXT
%       gluetostringc.EXT
%       isemptycell.EXT
%       isscalarcell.EXT
%       kendtaupairsign.EXT
%       mesh_morph.EXT
%       mesh_trianglestoneighbors.EXT
%       mesh_trimapmesh.EXT
%       minmaxmean.EXT
%       splittocellc.EXT
%       svmpredictc.EXT (not on Windows)
%       svmtrainc.EXT (not on Windows)
%       threshlutc.EXT
%       threshmapc.EXT
%
% If you wish to help the distributors of BVQXtools, please compile
% these files with Matlab v7.0 (R14) on your desired platform and
% send in the binary results to
%
% jw2661@columbia.edu
%
% Thank you!

% Version:  v0.8a
% Build:    9102222
% Date:     Oct-22 2009, 10:10 PM CEST
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% arguments
if nargin < 1
    hPrg = [];
end
if nargin < 2
    hBar = [];
end
if nargin < 3
    pRng = [0, 1];
end
pMin = pRng(1);
pDif = pRng(2) - pMin + eps;

% get old pwd
opwd = pwd;

% change to path
eoc = 0;
ler = cell(1, 1);
try
    cd(bvqxtools_path);
    cfiles = findfiles(pwd, '*.c');
    for cc = 1:numel(cfiles)
        if ~isempty(strfind(cfiles{cc}, 'svm'))
            continue;
        end
        [mfp, mfn, mfe] = fileparts(cfiles{cc});
        instprog(hPrg, hBar, sprintf('Compiling %s...', cfiles{cc}), ...
            pMin + pDif * (cc / (numel(cfiles) + 4)), ...
            ['Compiling MEX files: ' mfn '...']);
        try
            cd(mfp);
            mex('-O', [mfn mfe]);
        catch
            eoc = eoc + 1;
            ler{eoc} = lasterr;
        end
    end
    
    % libSVM files need special treatment, but only on non-Windows
    cd(bvqxtools_path);
    if isempty(strfind(lower(computer), 'pcwin'))
        instprog(hPrg, hBar, ...
            'Compiling libSVM files (svmpredictc / svmtrainc) with svm.cpp...', ...
            pMin + pDif * (numel(cfiles) / (numel(cfiles) + 3)));
        mex svmpredictc.c svm.cpp
        mex svmtrainc.c svm.cpp
    end
catch
    try
        cd(opwd);
    catch
        % do nothing
    end
    error( ...
        'BVQXtools:MEXError', ...
        'Custom error compiling MEX functions.' ...
    );
end

% change pwd back
cd(opwd);

% specific error message
if eoc > 0
    error( ...
        'BVQXtools:MEXError', ...
        'Error compiling at least one function.' ...
    );
end


% sub function for progress
function instprog(hedt, hbar, txt, p, ptxt)
if ~isempty(hedt)
    if ~isempty(txt)
        str = hedt.String;
        if ischar(str) && ...
            size(str, 1) > 1
            str = cellstr(str);
        elseif ischar(str)
            str = splittocell(str, char(10));
        end
        str = [str(:); lsqueeze(splittocell(txt, char(10)))];
        hedt.String = str;
        hedt.ListboxTop = max(1, numel(str) - 16);
    end
    if nargin > 4
        hbar.Progress(p, ptxt);
    elseif nargin > 3
        hbar.Progress(p);
    end
    drawnow;
else
    disp(txt);
end
